package com.movie.service;

import com.movie.dto.VipLevelDTO;
import com.movie.entity.VipLevel;
import com.movie.repository.VipLevelRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class VipLevelService {
    
    private final VipLevelRepository vipLevelRepository;
    
    public List<VipLevelDTO> findAll() {
        return vipLevelRepository.findAll()
                .stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    public List<VipLevelDTO> findActiveLevels() {
        return vipLevelRepository.findActiveLevels()
                .stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    public VipLevelDTO findById(Long id) {
        VipLevel vipLevel = vipLevelRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("VIP等级不存在"));
        return convertToDTO(vipLevel);
    }
    
    public VipLevelDTO findByName(String name) {
        VipLevel vipLevel = vipLevelRepository.findByName(name)
                .orElseThrow(() -> new RuntimeException("VIP等级不存在"));
        return convertToDTO(vipLevel);
    }
    
    public VipLevelDTO findByLevel(Integer level) {
        VipLevel vipLevel = vipLevelRepository.findByLevel(level)
                .orElseThrow(() -> new RuntimeException("VIP等级不存在"));
        return convertToDTO(vipLevel);
    }
    
    public VipLevelDTO create(VipLevelDTO vipLevelDTO) {
        // 检查等级名称是否已存在
        if (vipLevelRepository.findByName(vipLevelDTO.getName()).isPresent()) {
            throw new RuntimeException("VIP等级名称已存在");
        }
        
        // 检查等级值是否已存在
        if (vipLevelRepository.findByLevel(vipLevelDTO.getLevel()).isPresent()) {
            throw new RuntimeException("VIP等级值已存在");
        }
        
        VipLevel vipLevel = convertToEntity(vipLevelDTO);
        VipLevel saved = vipLevelRepository.save(vipLevel);
        return convertToDTO(saved);
    }
    
    public VipLevelDTO update(Long id, VipLevelDTO vipLevelDTO) {
        VipLevel existing = vipLevelRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("VIP等级不存在"));
        
        // 检查等级名称是否被其他等级使用
        if (!existing.getName().equals(vipLevelDTO.getName()) && 
            vipLevelRepository.findByName(vipLevelDTO.getName()).isPresent()) {
            throw new RuntimeException("VIP等级名称已存在");
        }
        
        // 检查等级值是否被其他等级使用
        if (!existing.getLevel().equals(vipLevelDTO.getLevel()) && 
            vipLevelRepository.findByLevel(vipLevelDTO.getLevel()).isPresent()) {
            throw new RuntimeException("VIP等级值已存在");
        }
        
        // 更新字段
        existing.setName(vipLevelDTO.getName());
        existing.setDescription(vipLevelDTO.getDescription());
        existing.setLevel(vipLevelDTO.getLevel());
        existing.setMonthlyPrice(vipLevelDTO.getMonthlyPrice());
        existing.setYearlyPrice(vipLevelDTO.getYearlyPrice());
        existing.setDiscountRate(vipLevelDTO.getDiscountRate());
        existing.setMaxWatchTime(vipLevelDTO.getMaxWatchTime());
        existing.setMaxDownloadCount(vipLevelDTO.getMaxDownloadCount());
        existing.setAdFree(vipLevelDTO.getAdFree());
        existing.setHdQuality(vipLevelDTO.getHdQuality());
        existing.setPrioritySupport(vipLevelDTO.getPrioritySupport());
        existing.setExclusiveContent(vipLevelDTO.getExclusiveContent());
        existing.setColor(vipLevelDTO.getColor());
        existing.setIconUrl(vipLevelDTO.getIconUrl());
        existing.setRequiredPoints(vipLevelDTO.getRequiredPoints());
        existing.setStatus(vipLevelDTO.getStatus());
        existing.setSortOrder(vipLevelDTO.getSortOrder());
        
        VipLevel updated = vipLevelRepository.save(existing);
        return convertToDTO(updated);
    }
    
    public void delete(Long id) {
        VipLevel vipLevel = vipLevelRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("VIP等级不存在"));
        vipLevelRepository.delete(vipLevel);
    }
    
    public VipLevelDTO toggleStatus(Long id) {
        VipLevel vipLevel = vipLevelRepository.findById(id)
                .orElseThrow(() -> new RuntimeException("VIP等级不存在"));
        vipLevel.setStatus(!vipLevel.getStatus());
        VipLevel updated = vipLevelRepository.save(vipLevel);
        return convertToDTO(updated);
    }
    
    private VipLevelDTO convertToDTO(VipLevel vipLevel) {
        VipLevelDTO dto = new VipLevelDTO();
        BeanUtils.copyProperties(vipLevel, dto);
        return dto;
    }
    
    private VipLevel convertToEntity(VipLevelDTO dto) {
        VipLevel vipLevel = new VipLevel();
        BeanUtils.copyProperties(dto, vipLevel);
        return vipLevel;
    }
}